local app = require 'app'
local game = app.game.running
local util = game.require 'util.util'
local align_helper = game.pokerlib.core.align_helper

local hand_sort = function(p1, p2)
    if p1.data.client_weight == p2.data.client_weight then
        return p1.data.client_type < p2.data.client_type
    else
        return p1.data.client_weight > p2.data.client_weight
    end
end

local chu_align = function(poker_t, parent, pivot)
    local card_ids = {}
    for _, poker in ipairs(poker_t) do
        table.insert(card_ids, poker.data.server_value)
    end
    local action = util.card_ids2action(card_ids)
    local config = game.config.card_type[action]
    if not config and not config[1] then
        align_helper.Align(poker_t, parent, align_helper.EPivot.middle, 50, 10, 100);
        return
    end

    config = config[1]

    local str_align = nil
    if config[4] then
        str_align = config[4]
    else
        str_align = action
    end

    local aligned_poker_t = {}
    for i = 1, #str_align do
        local server_symbol = string.sub(str_align, i, i)
        for j, poker in ipairs(poker_t) do
            if server_symbol == util.client2server_symbol[poker.data.client_value] then
                table.remove(poker_t, j)
                table.insert(aligned_poker_t, poker)
                break
            end
        end
    end

    for i, poker in ipairs(aligned_poker_t, poker) do
        poker_t[i] = poker
    end

    align_helper.Align(poker_t, parent, pivot, 30, 10, 10);
end

local chu_align_1 = function(poker_t, parent)
    chu_align(poker_t, parent, align_helper.EPivot.middle)
end

local chu_align_2 = function(poker_t, parent)
    chu_align(poker_t, parent, align_helper.EPivot.left)
end

local chu_align_3 = function(poker_t, parent)
    chu_align(poker_t, parent, align_helper.EPivot.right)
end

local on_poker_container_changed = function(poker_container)
    local selected_pokers = poker_container:get_selected_poker()
    game.store.pokers.on_selected_change(selected_pokers)
end

local config_1 = game.pokerlib.core.poker_container_config.new()
local config_2 = game.pokerlib.core.poker_container_config.new()
local config_3 = game.pokerlib.core.poker_container_config.new()

config_1.poker_data_class = game.require 'pokers.poker_data'
config_1.chu_poker_class = game.require 'pokers.chu_poker'
config_1.hand_poker_class = game.require 'pokers.hand_poker'
config_1.hand_sort = hand_sort
config_1.select_change = on_poker_container_changed
config_1.chu_align = chu_align_1

config_2.show_hand = false
config_2.ope_valid = false
config_2.poker_data_class = game.require 'pokers.poker_data'
config_2.chu_poker_class = game.require 'pokers.chu_poker'
config_2.hand_poker_class = game.require 'pokers.hand_poker'
config_2.chu_align = chu_align_2

config_3.show_hand = false
config_3.ope_valid = false
config_3.poker_data_class = game.require 'pokers.poker_data'
config_3.chu_poker_class = game.require 'pokers.chu_poker'
config_3.hand_poker_class = game.require 'pokers.hand_poker'
config_3.chu_align = chu_align_3

return {
    [1] = config_1,
    [2] = config_2,
    [3] = config_3,
}